﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditSlideShow : System.Web.UI.UserControl
    {
        public string nameslide;
        public string title;
        public string place;
        public string imageslide;
        public string alt;
        public string link;
        public string size;
        public int slideid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                slideid = Convert.ToInt32(Request.QueryString["idslide"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Slide", "Slide_ID", slideid.ToString());
                nameslide = dt.Rows[0]["Slide_TieuDe"].ToString();
                title = dt.Rows[0]["Slide_Title"].ToString();
                place = dt.Rows[0]["Slide_Place"].ToString();
                imageslide = dt.Rows[0]["Slide_Image"].ToString();
                alt = dt.Rows[0]["Slide_Alt"].ToString();
                link = dt.Rows[0]["Slide_Link"].ToString();
                size = dt.Rows[0]["Slide_Size"].ToString();
                txtName.Text = nameslide;
                txtTitle.Text = title;
                txtPlace.Text = place;
                txtAlt.Text = alt;
                txtLink.Text = link;
                txtSize.Text = size;
            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }
        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {

            if (txtLink.Text != "")
            {
                    if (fulImageSlide.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageSlide.FileName))
                        {
                            string linkimage = fulImageSlide.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Slide/"), linkimage, 940, fulImageSlide.PostedFile.InputStream);
                            filePathSlide = "/Resources/Images/Images-Slide/" + fulImageSlide.FileName;

                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[7];
                            cl[0] = new DAL.DHelpers.Column("Slide_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Slide_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Slide_Place", txtPlace.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Slide_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Slide_Alt", txtAlt.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Slide_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[6] = new DAL.DHelpers.Column("Slide_Size", txtSize.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            string[] colWhere = new string[1];
                            colWhere[0] = "Slide_ID";

                            object[] colValue = new object[1];
                            colValue[0] = slideid;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Slide", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                        cl[0] = new DAL.DHelpers.Column("Slide_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[1] = new DAL.DHelpers.Column("Slide_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[2] = new DAL.DHelpers.Column("Slide_Place", txtPlace.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[3] = new DAL.DHelpers.Column("Slide_Alt", txtAlt.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[4] = new DAL.DHelpers.Column("Slide_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[5] = new DAL.DHelpers.Column("Slide_Size", txtSize.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        string[] colWhere = new string[1];
                        colWhere[0] = "Slide_ID";

                        object[] colValue = new object[1];
                        colValue[0] = slideid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_Slide", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                    }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập link liên kết...')</script>");
            }
        }
    }
}